#include "ndk/strace.h"
#include "ndk/ndk_log.h"
#include "ndk/log_to_file.h"
#include "ndk/log_to_stdout.h"

namespace ndk
{

int ndk_log::apply_module_config(config_entry *entry)
{
  STRACE("");
  if (entry == 0) return -1;
  if (this->config_entry_ == 0)
  {
    this->config_entry_ = entry;
  }else if (*entry != *this->config_entry_)
  {
    delete this->config_entry_;
    this->config_entry_ = entry;
    return this->apply_module_config_i();
  }else
  {
    delete entry;
  }
  return 0;
}
int ndk_log::apply_module_config_i()
{
  STRACE("");
  // 1. Ouput module
  if (NDK_BIT_ENABLED(this->config_entry_->output_, 
                      config_entry::om_files))
  {
    if (this->files_output_ == 0)
    {
      this->files_output_ = new log_to_file();
      this->files_output_->open(this->config_entry_);
    }else
    {
      this->files_output_->update_config(this->config_entry_);
    }
  }else
  {
    if (this->files_output_)
    {
      delete this->files_output_;
      this->files_output_ = 0;
    }
  }
  //  
  if (NDK_BIT_ENABLED(this->config_entry_->output_, 
                      config_entry::om_stdout))
  {
    if (this->stdout_output_ == 0)
    {
      this->stdout_output_ = new log_to_stdout();
      this->stdout_output_->open(this->config_entry_);
    }else
    {
      this->stdout_output_->update_config(this->config_entry_);
    }
  }else
  {
    if (this->stdout_output_)
    {
      delete this->stdout_output_;
      this->stdout_output_ = 0;
    }
  }
  return 0;
}
int ndk_log::output(const char *record, const int len, int log_type)
{
  STRACE("");
  if (this->config_entry_->output_ == config_entry::om_null)
    return 0;
  int result = 0;
  if (NDK_BIT_ENABLED(this->config_entry_->output_,
                      config_entry::om_files))
  {
    result = this->files_output_->log(record, len + 1, log_type);
  }
  if (NDK_BIT_ENABLED(this->config_entry_->output_,
                      config_entry::om_stdout))
  {
    result = this->stdout_output_->log(record, len + 1, log_type);
  }
  return result;
}
size_t mtrace::count_ = 0;
} // namespace ndk

